package com.snoopy.gas1.controller;

import com.snoopy.gas1.entity.Station_info;
import com.snoopy.gas1.service.Station_infoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("station_info")
public class Station_infoController {
    @Autowired
    Station_infoService stationInfoService;

    @GetMapping("/list")
    public String list(Model model) {
        List<Station_info> station_infoList = stationInfoService.lists();
        model.addAttribute("stationLists", station_infoList);
        return "station_info/list";
    }

    // 显示新增表单 (GET)
    @GetMapping("/add")
    public String showAddForm(Model model) {
        model.addAttribute("station", new Station_info());
        return "Station_info/add";
    }

    // 处理新增提交 (POST)
    @PostMapping("/add")
    public String add(Station_info station) {
        stationInfoService.insert(station);
        return "redirect:/station_info/list";
    }

    @GetMapping("/edit/{id}")
    public String edit(@PathVariable Integer id, Model model) {
        Station_info station = stationInfoService.getById(id);
        model.addAttribute("station", station);
        return "station_info/edit";
    }

    @PostMapping("/update")
    public String update(Station_info station) {
        stationInfoService.update(station);
        return "redirect:/station_info/list";
    }

    @GetMapping("/delete/{id}")
    public String delete(@PathVariable Integer id) {
        stationInfoService.delete(id);
        return "redirect:/station_info/list";
    }

    @GetMapping("/search")
    public String search(@RequestParam String name, Model model) {
        List<Station_info> station_infoList = stationInfoService.searchByName(name);
        model.addAttribute("stationLists", station_infoList);
        return "station_info/list";
    }
    }
